﻿using Bussiness.Interface;
using Common;
using Common.Model;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Bussiness.Service
{
    public class ListInforService : BaseService,IListInforService
    {
        /// <summary>
        /// 分页查询返回LayuiJson数据
        /// </summary>
        /// <typeparam name="T">泛型参数</typeparam>
        /// <param name="page">第几页</param>
        /// <param name="limit">每页最大数量</param>
        /// <param name="expreesions">表达式数组</param>
        /// <returns></returns>
        public LayuiJson<T> ListInfor<T>(int page, int limit, params Expression<Func<T, bool>>[] expreesions)  where T : class
        {
            //总记录数
            int recordCount = 0;
            LayuiJson<T> layuiJson = new LayuiJson<T>();
            Result<List<T>> result = new Result<List<T>>();
            result = base.SelectPage<T>(page - 1, limit, out recordCount, expreesions);
            layuiJson.count = recordCount;
            layuiJson.msg = result.Msg;
            layuiJson.data = result.Data;
            return layuiJson;
        }

        /// <summary>
        /// 进行模型转换
        /// </summary>
        /// <typeparam name="TIn"></typeparam>
        /// <typeparam name="TOut"></typeparam>
        /// <returns></returns>
        public List<TOut> List<TIn, TOut>() where TIn : class
            where TOut : class
        {
            List<TOut> list = new List<TOut>();
            Result<List<TIn>> result = new Result<List<TIn>>();
            result = base.Select<TIn>();
            try
            {
                if (result.Data != null)
                {
                    foreach (var item in result.Data)
                    {
                        list.Add(ExpressionGenericMapper<TIn, TOut>.Trans(item));
                    }
                }
            }
            catch {
                list = null;
            }
            return list;
        }
    }
}
